package com.eastcom.groovy.task.taskdirector.temp

import java.awt.TexturePaintContext.Int;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress.Cache;
import java.sql.Date;
import java.sql.PreparedStatement
import java.sql.SQLException
import java.sql.Timestamp
import java.sql.Types;
import java.text.SimpleDateFormat
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.eastcom.groovy.tools.common.JmsJndiConfig
import com.eastcom.groovy.tools.common.MQCommunication
import com.eastcom.groovy.tools.common.ShellExecutor;
import com.eastcom.groovy.tools.db.DbEngine
import com.eastcom.groovy.tools.file.FileUtils;
import com.eastcom.groovy.tools.file.FtpUtils;
import com.eastcom.groovy.provider.*
import com.eastcom.groovy.tools.mail.*
import com.sun.java.util.jar.pack.Instruction.Switch;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.util.ClassPath.Dir;

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPFile
import org.apache.commons.net.ftp.FTPFileFilter
import org.apache.tools.ant.types.FileList;
import org.springframework.dao.DataAccessException
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback
import org.springframework.jdbc.support.lob.LobCreator
import org.springframework.jdbc.support.lob.OracleLobHandler
import org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
 * 任务类，可配置
 * @author yuanyue
 *
 */
class SimpleTxtFileTaskDirector_Ftp_KuanGuangGame_Top10_Hour {
	/************************配置属性，可以修改，请尽量使用英文防止中文编码问题***************************/
	final String ftpRemotePath = "/shanghai"
	final String ftpLocalPath = "/home/ipnet/mystope/data/hour"
	final String ftpFileNameRegex ="(\\d+)_(\\d+)_hourgame_data.csv"
	//没有时间的话用[]
	final String fileNameTimeFormat ="yyyyMMddHH"
	final String timeFormat = "yyyy-MM-dd HH:mm:ss"
	final String ftpIp = "221.176.3.165"
	final String ftpUser = "target"
	final String ftpPasswd = "password"
	final int ftpPort = 21
	final int ftpFileLimit = 200
	final String ftpSystem = "UNIX"
	final String ftpControlEnCode = "UTF-8"
	final String ftpFileEnCode = "UTF-8"
	final String ftpServerLanguage = "zh"
	final long fileLimit = 7200000
	final String dstdbBean = "oracle_39_nhm"
	final String regex ="(.+),(.+),(.+),(.+),(.+),(.+)"
	final int[] groupId =[1,2,3,4,5,6]
	final int parseLineNumOnce = 1
	final String insertSql = "insert into KUANGUANG_GAME_TOP10_HOUR(TIMESTAMP,GAMENAME,FLOWAVGIN,FLOWPEAKIN,FLOWAVGOUT,FLOWPEAKOUT) values(?,?,?,?,?,?)"
	/***************************可配置部分结束***********************************************/

















	static ThreadPoolTaskExecutor threadPoolTaskExecutor
	static List<String> dstFieldList = new ArrayList()
	static List<Integer> dstFieldTypeList = new ArrayList()
	static int[] sqlTypes
	List<List<String>> parseResultList = new ArrayList()
	static Log logger = LogFactory.getLog(SimpleTxtFileTaskDirector_Ftp_KuanGuangGame_Top10_Hour.class)



	/*********************initJob和doJob是要被程序调用的方法**************************/
	public boolean initJob(String jobId){
		doDbInit()
	}

	public boolean doJob(String jobId){
		doFtp()
		doParseFile()
	}

	private doDbInit(){
		try{
			String reg = "\\w+\\s+\\w+([^\\(\\),]+)\\s*\\(([^\\)]+)"
			String dstTb
			String dstFields
			Pattern pattern = Pattern.compile(reg)
			Matcher matcher = pattern.matcher(insertSql)
			if(matcher.find()){
				dstTb = matcher.group(1)
				dstFields = matcher.group(2)
			}
			DbEngine.loadDb(dstdbBean, dstTb, dstFields, dstFieldList, dstFieldTypeList, true)
			sqlTypes = new int[dstFieldTypeList.size()]
			for(int i = 0; i < dstFieldTypeList.size(); i++){
				sqlTypes[i] = dstFieldTypeList.get(i)
			}
		}catch(Exception e){
			e.printStackTrace()
			logger.error(e.getMessage())
		}
	}

	private boolean doFtp(){
		try{
			FTPFile[] acceptFiles = null;
			FtpUtils ftpUtils = new FtpUtils();
			String rp = ftpRemotePath
			String lp = ftpLocalPath
			File dirFile = new File(lp);
			if(! dirFile.exists()){
				dirFile.mkdirs();
			}
			ftpUtils.login(ftpIp, ftpPort, ftpUser, ftpPasswd, ftpSystem, ftpControlEnCode, ftpServerLanguage);
			ftpUtils.getFtp().changeWorkingDirectory(rp);
			String fileNameRegex = ftpFileNameRegex
			String fileTimeFormat = fileNameTimeFormat
			String nameRegex = fileNameRegex
			println nameRegex

			String timeFormat = fileTimeFormat
			Pattern pat = Pattern.compile(nameRegex);
			acceptFiles = ftpUtils.getFtp().listFiles(null, new FTPFileFilter() {
						private int fileCount = 0;
						@Override
						public boolean accept(FTPFile file) {
							// TODO 自动生成的方法存根
							String name = file.getName();
							println name

							Matcher mat = pat.matcher(name);
							boolean rs = mat.find();
							if(! rs){
								return false
							}
							String tm1 = mat.group(1)
							String tm2 = mat.group(2)
							SimpleDateFormat simp = new SimpleDateFormat(timeFormat)
							java.util.Date date = simp.parse(tm1+tm2)
							Calendar fileLimitTime = Calendar.getInstance();
							fileLimitTime.setTimeInMillis(new java.util.Date().getTime() - fileLimit);
							if(date.before(fileLimitTime.getTime())){
								return false
							}
							File localFile = new File(lp);
							String[] localFiles = localFile.list();
							if(localFiles != null){
								for(String lf: localFiles){
									if(lf.equalsIgnoreCase(name)){
										return false;
									}
								}
							}

							if(fileCount > ftpFileLimit){
								return false;
							}
							fileCount++;
							return true;
						}
					});
			boolean res = false;
			int count = 0;
			System.out.println("接收的文件数量："+acceptFiles.length);
			for(int k = 0; k < acceptFiles.length; k++){
				String fileName = acceptFiles[k].getName();
				File f = null;
				String dir = lp

				if(dir.endsWith("/")||dir.endsWith("\\")||dir.endsWith("\\\\")){
					f=new File(dir+fileName);
				}else{
					f=new File(dir+"/"+fileName);
				}
				OutputStream os=new FileOutputStream(f);
				res = ftpUtils.getFtp().retrieveFile(new String(fileName.getBytes(ftpFileEnCode),"ISO-8859-1"), os);
				if(res){
					count ++;
				}
				os.flush()
				os.close()
				logger.debug("下载了"+count+"文件！")
			}

		}catch (Exception e) {
			e.printStackTrace()
			logger.error(e.getMessage())
		}
	}

	private boolean doParseFile(){
		try{
			Calendar fileTime = Calendar.getInstance();
			Calendar fileLimitTime = Calendar.getInstance();
			fileLimitTime.setTimeInMillis(new java.util.Date().getTime() - fileLimit);
			File dirs = new File(ftpLocalPath)
			File[] dirFile = dirs.listFiles(new FilenameFilter() {

						@Override
						public boolean accept(File dir, String name) {
							// TODO 自动生成的方法存根
							if(name.matches(ftpFileNameRegex)){
								return true;
							}
							return false;
						}
					});
			for(File f: dirFile){
				long time = f.lastModified();
				fileTime.setTimeInMillis(time);
				if(fileTime.before(fileLimitTime)){
					continue;
				}
				String fileNameTime = null;


				BufferedReader reader = null;

				try {
					reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), ftpFileEnCode));
				} catch (Exception e) {
					e.printStackTrace()
				}
				String rg = regex
				int[] gd = groupId
				StringBuilder context = new StringBuilder();
				Pattern pat = Pattern.compile(rg);
				String line = null;
				boolean isFileEnd = false;
				int counts = 0
				boolean rs = false
				while(true){
					if(parseResultList.size() >= 15000){
						doInsertResult()
					}
					for(int i = 0; i < parseLineNumOnce; i++){
						try {
							line = reader.readLine()
							logger.debug("line="+line)
							if(line == null){
								isFileEnd = true
								break
							}
						} catch (Exception e) {
							e.printStackTrace()
						}

						context.append(line+"\n")
					}
					Matcher mat = pat.matcher(context.toString());
					rs = mat.find();
					if(rs){
						context = new StringBuilder();
						List<String> valueList = new ArrayList<String>();
						for(int g : gd){
							String ct = mat.group(g)
							valueList.add(ct);
						}
						parseResultList.add(valueList)
					}else{
						logger.debug("不匹配！"+regex+"-------"+context.toString());
					}
					if(isFileEnd){
						logger.debug("文件已经读完！");
						doInsertResult()
						break;
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace()
			logger.error(e.getMessage())
		}
	}

	private boolean doInsertResult(){
		try{
			DbEngine dbEngine = SpringContextProvider.getBean("dbEngine")
			dbEngine.getJdbcUtil().setDataSource(DynamicBeanDomianProvider.getBean(dstdbBean))
			for(List<String> raw: parseResultList){
				Object[] param = new Object[raw.size()]
				for(int i = 0; i < raw.size(); i++){
					print raw.get(i)+"--"+sqlTypes[i]+"\t"
					try {
						switch(sqlTypes[i]){
							case Types.INTEGER:
								param[i] = Integer.valueOf(raw.get(i))
								break
							case Types.FLOAT:
							case Types.DOUBLE:
								param[i] = Double.valueOf(raw.get(i))
								break
							case Types.DECIMAL:
							case Types.NUMERIC:
								param[i] = BigDecimal.valueOf(Double.valueOf(raw.get(i)))
								break
							case Types.TIMESTAMP:
								java.util.Date date = new SimpleDateFormat(timeFormat).parse(raw.get(i))
								logger.debug(date)
								param[i] = new Timestamp(date.getTime())
								break
							case Types.DATE:
								java.util.Date date = new SimpleDateFormat(timeFormat).parse(raw.get(i))
								logger.debug(date)
								param[i] = new Date(date.getTime())
								break
							case Types.VARCHAR:
							case Types.NVARCHAR:
								param[i] = raw.get(i)
								break
							case Types.OTHER:
								param[i] = raw.get(i)
								break;
							default:
								param[i] = null
								logger.warn("入库数据库字段无法识别！使用Null插入！")
								break;
						}
					} catch (Exception e) {
						e.printStackTrace()
						logger.error(e.getMessage())
						param[i]=null
					}
				}
				try{
					dbEngine.getJdbcUtil().update(insertSql, param, sqlTypes)
				}catch(Exception e){
					e.printStackTrace()
					logger.error(e.getMessage())
				}
				println ""
			}
			parseResultList.clear()
		}catch(Exception e){
			e.printStackTrace()
			logger.error(e.getMessage())
		}
	}

}